'\" t
.TH "SD_LOGIN_MONITOR_NEW" "3" "" "systemd 256.7" "sd_login_monitor_new"
.\" -----------------------------------------------------------------
.\" * Define some portability stuff
.\" -----------------------------------------------------------------
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.\" http://bugs.debian.org/507673
.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.ie \n(.g .ds Aq \(aq
.el       .ds Aq '
.\" -----------------------------------------------------------------
.\" * set default formatting
.\" -----------------------------------------------------------------
.\" disable hyphenation
.nh
.\" disable justification (adjust text to left margin only)
.ad l
.\" -----------------------------------------------------------------
.\" * MAIN CONTENT STARTS HERE *
.\" -----------------------------------------------------------------
.SH "NAME"
sd_login_monitor_new, sd_login_monitor_unref, sd_login_monitor_unrefp, sd_login_monitor_flush, sd_login_monitor_get_fd, sd_login_monitor_get_events, sd_login_monitor_get_timeout, sd_login_monitor \- Monitor login sessions, seats, users and virtual machines/containers
.SH "SYNOPSIS"
.sp
.ft B
.nf
#include <systemd/sd\-login\&.h>
.fi
.ft
.HP \w'int\ sd_login_monitor_new('u
.BI "int sd_login_monitor_new(const\ char\ *" "category" ", sd_login_monitor\ **" "ret" ");"
.HP \w'sd_login_monitor\ *sd_login_monitor_unref('u
.BI "sd_login_monitor *sd_login_monitor_unref(sd_login_monitor\ *" "m" ");"
.HP \w'void\ sd_login_monitor_unrefp('u
.BI "void sd_login_monitor_unrefp(sd_login_monitor\ **" "m" ");"
.HP \w'int\ sd_login_monitor_flush('u
.BI "int sd_login_monitor_flush(sd_login_monitor\ *" "m" ");"
.HP \w'int\ sd_login_monitor_get_fd('u
.BI "int sd_login_monitor_get_fd(sd_login_monitor\ *" "m" ");"
.HP \w'int\ sd_login_monitor_get_events('u
.BI "int sd_login_monitor_get_events(sd_login_monitor\ *" "m" ");"
.HP \w'int\ sd_login_monitor_get_timeout('u
.BI "int sd_login_monitor_get_timeout(sd_login_monitor\ *" "m" ", uint64_t\ *" "timeout_usec" ");"
.SH "DESCRIPTION"
.PP
\fBsd_login_monitor_new()\fR
may be used to monitor login sessions, users, seats, and virtual machines/containers\&. Via a monitor object a file descriptor can be integrated into an application defined event loop which is woken up each time a user logs in, logs out or a seat is added or removed, or a session, user, seat or virtual machine/container changes state otherwise\&. The first parameter takes a string which can be
"seat"
(to get only notifications about seats being added, removed or changed),
"session"
(to get only notifications about sessions being created or removed or changed),
"uid"
(to get only notifications when a user changes state in respect to logins) or
"machine"
(to get only notifications when a virtual machine or container is started or stopped)\&. If notifications shall be generated in all these conditions,
\fBNULL\fR
may be passed\&. Note that in the future additional categories may be defined\&. The second parameter returns a monitor object and needs to be freed with the
\fBsd_login_monitor_unref()\fR
call after use\&.
.PP
\fBsd_login_monitor_unref()\fR
may be used to destroy a monitor object\&. Note that this will invalidate any file descriptor returned by
\fBsd_login_monitor_get_fd()\fR\&.
.PP
\fBsd_login_monitor_unrefp()\fR
is similar to
\fBsd_login_monitor_unref()\fR
but takes a pointer to a pointer to an
\fBsd_login_monitor\fR
object\&. This call is useful in conjunction with GCC\*(Aqs and LLVM\*(Aqs
\m[blue]\fBClean\-up Variable Attribute\fR\m[]\&\s-2\u[1]\d\s+2\&. Note that this function is defined as inline function\&. Use a declaration like the following, in order to allocate a login monitor object that is freed automatically as the code block is left:
.sp
.if n \{\
.RS 4
.\}
.nf
{
  __attribute__((cleanup(sd_login_monitor_unrefp))) sd_login_monitor *m = NULL;
  int r;
  \&...
  r = sd_login_monitor_new(NULL, &m);
  if (r < 0) {
    errno = \-r;
    fprintf(stderr, "Failed to allocate login monitor object: %m\en");
  }
  \&...
}
.fi
.if n \{\
.RE
.\}
.PP
\fBsd_login_monitor_flush()\fR
may be used to reset the wakeup state of the monitor object\&. Whenever an event causes the monitor to wake up the event loop via the file descriptor this function needs to be called to reset the wake\-up state\&. If this call is not invoked, the file descriptor will immediately wake up the event loop again\&.
.PP
\fBsd_login_monitor_unref()\fR
and
\fBsd_login_monitor_unrefp()\fR
execute no operation if the passed in monitor object is
\fBNULL\fR\&.
.PP
\fBsd_login_monitor_get_fd()\fR
may be used to retrieve the file descriptor of the monitor object that may be integrated in an application defined event loop, based around
\fBpoll\fR(2)
or a similar interface\&. The application should include the returned file descriptor as wake\-up source for the events mask returned by
\fBsd_login_monitor_get_events()\fR\&. It should pass a timeout value as returned by
\fBsd_login_monitor_get_timeout()\fR\&. Whenever a wake\-up is triggered the file descriptor needs to be reset via
\fBsd_login_monitor_flush()\fR\&. An application needs to reread the login state with a function like
\fBsd_get_seats\fR(3)
or similar to determine what changed\&.
.PP
\fBsd_login_monitor_get_events()\fR
will return the
\fBpoll()\fR
mask to wait for\&. This function will return a combination of
\fBPOLLIN\fR,
\fBPOLLOUT\fR
and similar to fill into the
"\&.events"
field of
\fIstruct pollfd\fR\&.
.PP
\fBsd_login_monitor_get_timeout()\fR
will return a timeout value for usage in
\fBpoll()\fR\&. This returns a value in microseconds since the epoch of
\fBCLOCK_MONOTONIC\fR
for timing out
\fBpoll()\fR
in
\fItimeout_usec\fR\&. See
\fBclock_gettime\fR(2)
for details about
\fBCLOCK_MONOTONIC\fR\&. If there is no timeout to wait for this will fill in
\fB(uint64_t) \-1\fR
instead\&. Note that
\fBpoll()\fR
takes a relative timeout in milliseconds rather than an absolute timeout in microseconds\&. To convert the absolute \*(Aqμs\*(Aq timeout into relative \*(Aqms\*(Aq, use code like the following:
.sp
.if n \{\
.RS 4
.\}
.nf
uint64_t t;
int msec;
sd_login_monitor_get_timeout(m, &t);
if (t == (uint64_t) \-1)
  msec = \-1;
else {
  struct timespec ts;
  uint64_t n;
  clock_gettime(CLOCK_MONOTONIC, &ts);
  n = (uint64_t) ts\&.tv_sec * 1000000 + ts\&.tv_nsec / 1000;
  msec = t > n ? (int) ((t \- n + 999) / 1000) : 0;
}
.fi
.if n \{\
.RE
.\}
.PP
The code above does not do any error checking for brevity\*(Aqs sake\&. The calculated
\fImsec\fR
integer can be passed directly as
\fBpoll()\fR\*(Aqs timeout parameter\&.
.SH "RETURN VALUE"
.PP
On success,
\fBsd_login_monitor_new()\fR,
\fBsd_login_monitor_flush()\fR
and
\fBsd_login_monitor_get_timeout()\fR
return 0 or a positive integer\&. On success,
\fBsd_login_monitor_get_fd()\fR
returns a Unix file descriptor\&. On success,
\fBsd_login_monitor_get_events()\fR
returns a combination of
\fBPOLLIN\fR,
\fBPOLLOUT\fR
and suchlike\&. On failure, these calls return a negative errno\-style error code\&.
.PP
\fBsd_login_monitor_unref()\fR
always returns
\fBNULL\fR\&.
.SS "Errors"
.PP
Returned errors may indicate the following problems:
.PP
\fB\-EINVAL\fR
.RS 4
An input parameter was invalid (out of range, or
\fBNULL\fR, where that is not accepted)\&. The specified category to watch is not known\&.
.RE
.PP
\fB\-ENOMEM\fR
.RS 4
Memory allocation failed\&.
.RE
.SH "NOTES"
.PP
Functions described here are available as a shared library, which can be compiled against and linked to with the
\fBlibsystemd\fR\ \&\fBpkg-config\fR(1)
file\&.
.PP
The code described here uses
\fBgetenv\fR(3), which is declared to be not multi\-thread\-safe\&. This means that the code calling the functions described here must not call
\fBsetenv\fR(3)
from a parallel thread\&. It is recommended to only do calls to
\fBsetenv()\fR
from an early phase of the program when no other threads have been started\&.
.SH "HISTORY"
.PP
\fBsd_login_monitor_get_events()\fR
and
\fBsd_login_monitor_get_timeout()\fR
were added in version 201\&.
.PP
\fBsd_login_monitor_unrefp()\fR
was added in version 229\&.
.SH "SEE ALSO"
.PP
\fBsystemd\fR(1), \fBsd-login\fR(3), \fBsd_get_seats\fR(3), \fBpoll\fR(2), \fBclock_gettime\fR(2)
.SH "NOTES"
.IP " 1." 4
Clean-up Variable Attribute
.RS 4
\%https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html
.RE
